package com.itheima.webaitliasserver.controller;

import com.itheima.webaitliasserver.pojo.Emp;
import com.itheima.webaitliasserver.pojo.EmpEntryParam;
import com.itheima.webaitliasserver.pojo.PageResult;
import com.itheima.webaitliasserver.pojo.Result;
import com.itheima.webaitliasserver.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @ClassName: xiaoZhang
 * @Description:
 * @Author: Zhang
 * @CreateDate: 2025-07-23 15:21
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result queryAllEmpList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        // 查询所有员工列表记录数
        PageResult<Emp> pageResult = empService.queryEmpList(page , pageSize); // 查询所有员工
        return Result.success(pageResult);
    }
    /**
    * @Author: Zhang
    * @param:        
    * @createTime: 2025/7/25 16:49
    * @Description: 使用pageHelper 进行员工分页查询
    * @Return: 无
    */
    @GetMapping("/pageHelpers")
    public Result queryAllEmpListUsePageHelper(@RequestParam(defaultValue = "1") Integer page,   @RequestParam(defaultValue = "10") Integer pageSize) {
        System.out.println("queryAllEmpListUsePageHelper" + page + "----" + pageSize);
        PageResult<Emp> pageResult = empService.queryEmpListUsePageHelperService(page, pageSize);
        return Result.success(pageResult);
    }

    // 根据选项进行查询员工信息 支持分页
    @GetMapping("/optionsQuery")
    public Result queryEmpInfoByOption(EmpEntryParam entryParam) {
        log.info("queryEmpInfoByOption:{}" , entryParam);
        PageResult<Emp> empPageResult = empService.queryEmpByOptions(entryParam);
        return Result.success(empPageResult);
    }
    // 添加员工信息
    @PostMapping
    public Result addEmpInfo(@RequestBody  Emp emp) {
        empService.addEmpRecord(emp);
        return  Result.success();
    }

    @DeleteMapping
    public Result deleteEmpInfo(Integer[] ids) {
        log.info("deleteEmpInfo:{}" , Arrays.toString(ids));
        empService.removeEmpRecordById(ids);
        return  Result.success("success");
    }
    // 路径参数 id存在与路径上
    @GetMapping("/query/{id}")
    public Result queryEmpById(@PathVariable Integer id) {
        log.info("queryEmpById:{}" , id);
        // 使用id查询员工信息 回显数据
        if(id == null){
            Result.error("Id不能为空");
        }
        Emp empInfo = empService.queryEmpRecordById(id);
        return Result.success(empInfo);
    }
    
    /**
    * @Author: Zhang
    * @param:        
    * @createTime: 2025/9/1 13:47
    * @Description: 更新员工信息
    * @Return: 无
    */
    @PutMapping
    public Result updateEmpInfo(@RequestBody Emp emp) {
        empService.updateEmpRecord(emp);
        return Result.success("success");
    }

}
